package com.github.springcloud.order.service;

import com.github.springcloud.good.vo.GoodVo;
import com.github.springcloud.order.api.OrderService;
import com.github.springcloud.order.client.GoodClient;
import com.github.springcloud.order.dto.CreateOrderDto;
import com.github.springcloud.order.po.Order;
import com.github.springcloud.order.vo.OrderVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by zhaozuowen on 2018/7/12.
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private GoodClient goodClient;

    public static final ConcurrentHashMap<String,Order> orderStorage=new ConcurrentHashMap<>();

    @Override
    public void createOrder(CreateOrderDto createOrderDto) {

        GoodVo goodVo=goodClient.getByGoodNo(createOrderDto.getGoodNo());

        if(null==goodVo){
            throw new RuntimeException("商品不存在");
        }
        Order order=new Order();

        order.setId(UUID.randomUUID().toString());
        order.setGoodId(goodVo.getId());
        order.setGoodName(goodVo.getName());
        order.setOrderNo(UUID.randomUUID().toString());
        order.setUserId(createOrderDto.getUserId());

        orderStorage.put(order.getOrderNo(),order);





    }

    @Override
    public OrderVo getByOrderNo(String orderNo) {

        Order order=orderStorage.get(orderNo);

        if(null==order){
            throw new RuntimeException("订单不存在");
        }

        OrderVo orderVo=new OrderVo();

        BeanUtils.copyProperties(order,orderVo);

        return orderVo;
    }
}
